package com.qs.product.controller;


import com.qs.product.common.DecreaseStockInput;
import com.qs.product.common.ProductInfoOutput;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.qs.product.dataobject.ProductCategory;
import com.qs.product.dataobject.ProductInfo;
import com.qs.product.service.CategoryService;
import com.qs.product.service.ProductService;
import com.qs.product.util.ResultVoUtil;
import com.qs.product.vo.ProductInfoVo;
import com.qs.product.vo.ProductVo;
import com.qs.product.vo.ResultVo;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Desc: 商品
 * @author: jianfeng
 * @Time: 2019/9/9 15:57
 */
@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @Autowired
    private CategoryService categoryService;
    

//    @CrossOrigin(allowCredentials = "true") //允许跨域，allowCredentials = "true"，允许cookie跨域
    @RequestMapping("/list")
    public ResultVo<ProductVo> list() {
        //1.查询所有在架得商品
        List<ProductInfo> productInfoList = productService.findUpAll();

        //2.获取类目type
        List<Integer> categoryTypeList = productInfoList.stream()
                .map(ProductInfo::getCategoryType)
                .collect(Collectors.toList());

        //3.从数据库查询表类目
        List<ProductCategory> categoryList = categoryService.findByCategoryTypeIn(categoryTypeList);

        //4.构造数据
        List<ProductVo> productVoList = new ArrayList<>();
        for (ProductCategory productCategory : categoryList) {
            ProductVo productVo = new ProductVo();
            productVo.setCategoryName(productCategory.getCategoryName());
            productVo.setCategoryType(productCategory.getCategoryType());

            List<ProductInfoVo> productInfoVoList = new ArrayList<>();
            for (ProductInfo productInfo : productInfoList) {
                if (productInfo.getCategoryType().equals(productCategory.getCategoryType())) {
                    ProductInfoVo productInfoVo = new ProductInfoVo();
                    BeanUtils.copyProperties(productInfo, productInfoVo);
                    productInfoVoList.add(productInfoVo);
                }

            }
            productVo.setProductInfoVoList(productInfoVoList);
            productVoList.add(productVo);
        }

        return ResultVoUtil.success(productVoList);
    }

    /**
     * @param productIdList
     * @return java.util.List<com.qs.com.qs.product.dataobject.ProductInfo>
     * @Desc: 获取商品列表（给订单服务用）
     * @author: jianfeng
     * @Time: 2019/9/17 16:41
     */
    @PostMapping("/listForOrder")
    public List<ProductInfoOutput> listForOrder(@RequestBody List<String> productIdList) {
        return productService.findList(productIdList);
    }

    /**
     * @param decreaseStockInputList
     * @return void
     * @Desc: 扣库存
     * @author: jianfeng
     * @Time: 2019/9/17 17:06
     */
    @PostMapping("/decreaseStock")
    public void decreaseStock(@RequestBody List<DecreaseStockInput> decreaseStockInputList) {
        productService.decreaseStock(decreaseStockInputList);
    }

}
